import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
// 引入全局的store
import store from "@/store/index.js"
Vue.use(VueRouter)

const routes = [{
    path: '/',
    name: 'Login',
        meta: {
          name: "登录页",
          path: '/',
        },
    component: Login
  },
  {
    path: '/home',
    name: 'Home',
    redirect: "/index",
    // rediirect:"/forms"
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import( /* webpackChunkName: "about" */ '../views/Home.vue'),

    children: [{
        path: '/index',
        name: 'Index',
        meta: {
          name: "首页",
          path: '/index',
        },
        component: () => import( /* webpackChunkName: "about" */ '../views/Index.vue')
      },
      {
        path: '/forms',
        name: 'Forms',
        meta: {
          name: "froms页",
          path: '/forms',
        },
        component: () => import( /* webpackChunkName: "about" */ '../views/Forms.vue')
      },
      {
        path: '/icon',
        name: 'Icon',
        meta: {
          name: "icon页面",
          path: '/icon',
        },
        component: () => import( /* webpackChunkName: "about" */ '../views/Icon.vue')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
// 全局的守卫
router.beforeEach((to, from, next) => {
  console.log(to.meta.name);
  // console.log(store.state.routerData);
  if(to.meta.name=="登录页"){
    next()
    return false
  }
  let key = to.meta

  let data1 = store.state.routerData
  let index = data1.findIndex((item) => {
    return key.name == item.name
  })
  //不会重复追加
  if (index == -1) {
    // 向路由数组里面追加数据
    key.show = true
    store.commit("addRouter", key)
  }else {
    //点击切换样式
    store.commit("changeStyle",key)
  }



  // console.log(store.data);
  next()
})


const originalPush = VueRouter.prototype.push

VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
export default router